package cn.kinoko.service.cnTeam.impl;

import cn.kinoko.common.aspect.annotation.Cacheable;
import cn.kinoko.common.base.model.BaseServiceImpl;
import cn.kinoko.common.constant.CacheKeyConstant;
import cn.kinoko.common.utils.ExceptionUtil;
import cn.kinoko.mapper.cnTeam.CnTeamMemberMapper;
import cn.kinoko.model.cnTeam.entity.CnTeamMember;
import cn.kinoko.model.cnTeam.vo.MemberVO;
import cn.kinoko.service.cnTeam.PositionService;
import cn.kinoko.service.cnTeam.CnTeamMemberService;
import cn.kinoko.service.common.CommonService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kinoko
 */
@Service
@AllArgsConstructor
public class CnTeamMemberServiceImpl extends BaseServiceImpl<CnTeamMemberMapper, CnTeamMember> implements CnTeamMemberService {

    private PositionService cnPositionService;
    private CommonService commonService;

    /**
     * 根据用户id查询加入的汉化组id
     *
     * @param uid 用户id
     * @return 加入的汉化组id
     */
    @Cacheable(key = CacheKeyConstant.JOIN_TEAM_KEY, params = "#uid")
    @Override
    public List<Long> queryJoinTeamByUid(Long uid) {
        return lambdaQuery().select(CnTeamMember::getTId).eq(CnTeamMember::getUid, uid).list().stream()
                .map(CnTeamMember::getTId).toList();
    }

    /**
     * 根据汉化组id查询汉化组成员
     *
     * @param tid 汉化组id
     * @return 汉化组成员
     */
    @Override
    public List<MemberVO> queryTeamMember(Long tid) {
        // 查询汉化组成员
        List<MemberVO> memberList = getBaseMapper().queryTeamMember(tid);
        // 处理成员职位
        return commonService.handleMemberPosition(memberList);
    }

    /**
     * 查询成员职位
     *
     * @param tid 汉化组id
     * @param uid 用户id
     * @return 职位
     */
    @Override
    public Long queryPosition(Long tid, Long uid) {
        CnTeamMember cnTeamMember = lambdaQuery().select(CnTeamMember::getPosition)
                .eq(CnTeamMember::getTId, tid).eq(CnTeamMember::getUid, uid)
                .one();
        ExceptionUtil.isNull(cnTeamMember, "当前成员不在组内");
        return cnTeamMember.getPosition();
    }
}